package com.ss.android.pay;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.text.TextUtils;

import com.bytedance.common.utility.collection.WeakHandler;
import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.UIUtils;
import com.ss.android.article.browser.R;
import com.tencent.mm.opensdk.openapi.IWXAPI;

import java.util.HashMap;
import java.util.Map;

public abstract class a extends Service implements WeakHandler.Callback {
    final class a$a implements j {
        private final a b;
        private String a;

        public a$a(a arg1, String arg2) {
            this.b = arg1;
            this.a = arg2;
        }

        public final void a(int arg4, String arg5) {
            try {
                Bundle v0_1 = new Bundle();
                v0_1.putString("prepayid", this.a);
                v0_1.putString("result", arg5);
                Message v1 = Message.obtain(null, 1);
                v1.obj = v0_1;
                this.b.a.send(v1);
            } catch (Throwable v0) {
            }
        }
    }

    Messenger a;
    private Map b;
    private Map c;

    public a() {

        this.b = new HashMap();
        this.c = new HashMap();
    }

    public abstract IWXAPI a();

    public void handleMsg(Message message) {
        a$a v1_8;
        a$a v3_2 = null;
        String v4;
        String v3;
        String v6;
        Object v1_1;
        Messenger v5;
        a$a v2 = null;
        try {
            Logger.d("WXPayService", "handleMsg msg.what = " + message.what);
            switch (message.what) {
                case 0: {
                    if (message.replyTo == null) {
                        return;
                    }

                    v5 = message.replyTo;
                    v1_1 = message.obj;
                    v6 = ((Bundle) v1_1).getString("prepayid");
                    v3 = ((Bundle) v1_1).getString("request");
                    if (v3 != null) {
                        v4 = android.support.design.a.i(v3);
                    } else {
                        v4 = v3;
                    }
                    String v7 = ((Bundle) v1_1).getString("taskintent");
                    if ((TextUtils.isEmpty(((CharSequence) v6))) || (TextUtils.isEmpty(((CharSequence) v4)))) {
                        if (!this.b.isEmpty()) {
                            return;
                        }

                        this.stopSelf();
                        return;
                    }

                    Logger.d("WXPayService", "prePayId = " + v6 + " send_request request = " + v4);
                    this.b.put(v6, v5);
                    if (TextUtils.isEmpty(((CharSequence) v6))) {
                        return;
                    }

                    if (v5 == null) {
                        return;
                    }

                    if (TextUtils.isEmpty(((CharSequence) v4))) {
                        return;
                    }

                    IWXAPI v1_3 = this.a();
                    try {
                        v3_2 = new a$a(this, v6);
                        this.c.put(v6, v3_2);
                        l v2_3 = k.a().a(null, v1_3, v4, ((j) v3_2));
                        if ((v2_3 instanceof m)) {
                            ((m) v2_3).b = true;
                            ((m) v2_3).d = v7;
                        }
                        if (v2_3 == null) {
                            return;
                        }
                        v2_3.b();
                        return;
                    } catch (UnsupportedPayException v1_4) {
                        v1_4.printStackTrace();
                        if (v3_2 == null) {
                            return;
                        }
                        v3_2.a(-2, String.valueOf("-1"));
                        return;
                    } catch (PayException v1_7) {
                        if (v1_7.getErrResId() > 0) {
                            UIUtils.showImageToast(((Context) this), R.drawable.close_popup_textpage, v1_7.getErrResId());
                        }

                        if (v3_2 == null) {
                            return;
                        }

                        v3_2.a(-2, String.valueOf("-1"));
                        return;
                    } catch (WXNotInstalledException v1_5) {
                        v1_8 = v3_2;
                        int v2_4 = R.drawable.close_popup_textpage;
                        int v3_3 = R.string.toast_weixin_not_install;
                        UIUtils.showImageToast(((Context) this), v2_4, v3_3);
                        if (v1_8 == null) {
                            return;
                        }
                        v1_8.a(-2, "-1");
                        return;
                    } catch (Exception v1_6) {
                        if (v3_2 == null) {
                            return;
                        }
                        v3_2.a(-2, String.valueOf("-1"));
                        return;
                    }
                }
                case 1: {
                    v1_1 = message.obj;
                    String v2_1 = ((Bundle) v1_1).getString("prepayid");
                    v3 = ((Bundle) v1_1).getString("result");
                    if (TextUtils.isEmpty(((CharSequence) v2_1))) {
                        if (!this.b.isEmpty()) {
                            return;
                        }

                        this.stopSelf();
                        return;
                    }

                    Logger.d("WXPayService", "prePayId = " + v2_1 + " notify result = " + v3);
                    Object v2_2 = this.b.get(v2_1);
                    if (v2_2 == null) {
                        return;
                    }

                    Message v3_1 = Message.obtain(null, 1);
                    v3_1.obj = v1_1;
                    ((Messenger) v2_2).send(v3_1);
                    return;
                }
                case 2: {
                    String v1_2 = ((Bundle) message.obj).getString("prepayid");
                    if (TextUtils.isEmpty(((CharSequence) v1_2))) {
                        if (!this.b.isEmpty()) {
                            return;
                        }

                        this.stopSelf();
                        return;
                    }
                    Logger.d("WXPayService", "prePayId = " + v1_2 + " notify received");
                    if (this.b.containsKey(v1_2)) {
                        this.b.remove(v1_2);
                    }

                    if (!this.c.containsKey(v1_2)) {
                        return;
                    }

                    this.c.remove(v1_2);
                    return;
                }
            }

            return;
        } catch (Throwable v1) {
            if (!this.b.isEmpty()) {
                return;
            }
            this.stopSelf();
        }
    }

    public IBinder onBind(Intent arg4) {
        Logger.d("WXPayService", "onBind " + this);
        return this.a.getBinder();
    }

    public void onCreate() {
        Logger.d("WXPayService", "onCreate " + this);
        this.a = new Messenger(new WeakHandler(((WeakHandler.Callback) this)));
    }

    public void onDestroy() {
        super.onDestroy();
    }

    public boolean onUnbind(Intent arg4) {
        Logger.d("WXPayService", "onUnBind " + this);
        if (this.b.isEmpty()) {
            this.stopSelf();
        }

        return super.onUnbind(arg4);
    }
}

